# !pip install folium==0.5.0
import pandas as pd
import numpy as np
import folium
import json
folium.__version__
df = pd.read_csv('drive/My Drive/datasets/serie_historica_acumulados.csv',
encoding='latin-1', skipfooter=5, engine='python')
df.head()
df.tail()
df.info()
# loading geojson file
geojson_file = 'drive/My Drive/datasets/shapefiles_ccaa_espana.geojson'
with open(geojson_file, 'r') as file:
geo_data = json.loads(file.read())
# key of regions in the file
geo_regs = [i['properties']['hasc_1'] for i in geo_data['features']]
# getting the max of each column per region, since the data is accumulated
total_df = df.copy().groupby(['CCAA']).max().reset_index()
# Modefiying region name in dataframe to match geojson file
total_df['CCAA'] = 'ES.' + total_df['CCAA']
total_df.head()
# checking for difference in regions in both dataframe and geojson data
# to use it as key
different_regs = set(geo_regs).difference(set(total_df['CCAA']))
different_regs
# getting index of different regions in geojson data
# to delete them
for i in range(len(geo_data['features'])):
idx = []
if geo_data['features'][i]['properties']['hasc_1'] in list(different_regs):
idx.append(i)
# delete different regions from geo_data
for i in idx:
del geo_data['features'][i]['properties']['hasc_1']
# threshold scale for total cases
threshold_scale = np.linspace(total_df['CASOS'].min(),
total_df['CASOS'].max()+1, 6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale
# Choropleth map for total cases
spain_map = folium.Map([40.4637, -3.7492], zoom_start=6)
spain_map.choropleth(
geo_data=geo_data,
data=total_df,
columns=['CCAA', 'CASOS'],
key_on='properties.hasc_1',
threshold_scale=threshold_scale,
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.3,
legend_name='Total Covid19 Cases in Spain'
)
spain_map
# threshold scale for total PCR+
threshold_scale = np.linspace(total_df['PCR+'].min(),
total_df['PCR+'].max()+1, 6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale
# Choropleth map for total PCR+
spain_map = folium.Map([40.4637, -3.7492], zoom_start=6)
spain_map.choropleth(
geo_data=geo_data,
data=total_df,
columns=['CCAA', 'PCR+'],
key_on='properties.hasc_1',
threshold_scale=threshold_scale,
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.3,
legend_name='Total PCR+ Covid19 Cases in Spain'
)
spain_map
# threshold scale for total Hospitalized
threshold_scale = np.linspace(total_df['Hospitalizados'].min(),
total_df['Hospitalizados'].max()+1, 6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale
# Choropleth map for total Hospitalized
spain_map = folium.Map([40.4637, -3.7492], zoom_start=6)
spain_map.choropleth(
geo_data=geo_data,
data=total_df,
columns=['CCAA', 'Hospitalizados'],
key_on='properties.hasc_1',
threshold_scale=threshold_scale,
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.3,
legend_name='Total Hospitalized Covid19 Cases in Spain'
)
spain_map
# threshold scale for total deaths
threshold_scale = np.linspace(total_df['Fallecidos'].min(),
total_df['Fallecidos'].max()+1, 6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale
# Choropleth map for total deaths
spain_map = folium.Map([40.4637, -3.7492], zoom_start=6)
spain_map.choropleth(
geo_data=geo_data,
data=total_df,
columns=['CCAA', 'Fallecidos'],
key_on='properties.hasc_1',
threshold_scale=threshold_scale,
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.3,
legend_name='Total Covid19 Deaths in Spain'
)
spain_map
# threshold scale for total recoveries
threshold_scale = np.linspace(total_df['Recuperados'].min(),
total_df['Recuperados'].max()+1, 6, dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale
# Choropleth map for total recoveries
spain_map = folium.Map([40.4637, -3.7492], zoom_start=6)
spain_map.choropleth(
geo_data=geo_data,
data=total_df,
columns=['CCAA', 'Recuperados'],
key_on='properties.hasc_1',
threshold_scale=threshold_scale,
fill_color='OrRd',
fill_opacity=0.8,
line_opacity=0.3,
legend_name='Total Covid19 Recoveries in Spain'
)
spain_map